<!--
 * @Description:自定义滚动条
 * @Author: Sun
 * @Date: 2019-07-01 20:05:50
 * @LastEditors: Sun
 * @LastEditTime: 2019-07-09 22:37:12
 -->
<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title>div带框的拖拽</title>
	<style>
		#parent{
			width: 600px;
			height: 20px;
			background:#ccc;
			position: relative;
			margin: 0 auto;
		}

		#div1 {
			width: 20px;
			height: 20px;
			background:red;
			position: absolute;
			left: 0;
			top: 0;
		}

		/* #div2 {
		width: 0px;
		height: 0px;
		background:green;
		} */
		#div2 {
			width: 300px;
			height: 300px;
			background:green;
			opacity: .3;
		}

	</style>
	<script>
		window.onload = function () {
			var odiv = document.getElementById("div1")
			var odiv2 = document.getElementById("div2")
			var oparent = document.getElementById("parent")
			var disX = 0;
			var disY = 0;
			odiv.onmousedown = function (ev) {
				var oEvent = ev || event;
				disX = oEvent.clientX - odiv.offsetLeft;
				// disY = oEvent.clientY - odiv.offsetTop;
				document.onmousemove = function (ev) {
					var oEvent = ev || event;
					// i t 是鼠标距离div的内部距离
					var i = oEvent.clientX - disX
					// var t = oEvent.clientY - disY
					if(i<0){
						i=0
					}else if(i>oparent.offsetWidth-odiv.offsetWidth){
           i=oparent.offsetWidth-odiv.offsetWidth
					}
					odiv.style.left = i + 'px';
					var scale=i/(oparent.offsetWidth-odiv.offsetWidth)
					console.log(i)
					console.log(scale)
					odiv2.style.width=400*scale+'px'
					odiv2.style.height=400*scale+'px'
				  odiv2.style.opacity=scale
				}
				document.onmouseup = function () {
					document.onmousemove = null
					document.onmouseup = null
				}
				return false;
			}
		}
	</script>
</head>

<body>
	<div id="parent">
		<div id="div1"></div>
	</div>
	<div id="div2"></div>
	</div>
</body>

</html>